package com.xgz.work;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;

public class TestStu {
    public static void main(String[] args) {
        //操作propreties文件
        Properties properties = new Properties();
        //获取配置文件的输入流
        ClassLoader classLoader = Student.class.getClassLoader();
        InputStream resourceAsStream = classLoader.getResourceAsStream("reflect.properties");
        //加载配置文件信息
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据key获取文件中的值
        String classname = properties.getProperty("classname");
        System.out.println("classname = " + classname);
        String methodname = properties.getProperty("methodname");
        System.out.println("methodname = " + methodname);
        //利用反射获取class对象
        try {
            Class<?> aClass = Class.forName(classname);
            //获取class对象里对应方法
            Method declaredMethod = aClass.getDeclaredMethod(methodname);
            //执行方法
            //取消安全检查
            declaredMethod.setAccessible(true);
            Object o = aClass.newInstance();
            declaredMethod.invoke(o);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
